package com.zxmr.hospital.customer.action;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.Customer;
import com.zxmr.domain.SurgeryRecord;
import com.zxmr.domain.service.CustomerService;
import com.zxmr.domain.service.SurgeryRecordSearchBean;
import com.zxmr.domain.service.SurgeryRecordService;
import com.zxmr.hospital.customer.dto.JSONListDTO;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.util.Utils;

public class QueryAllSurgeryRecord extends Executor{

	@Override
	public void execute(Request request, Response response) {
		

		try
		{
			queryAll(request,response);

		}catch(Exception e)
		{
			ResponsePayload payload = new ResponsePayload();
			if(e instanceof ExecuteException)
			{
				payload.setValue(e.getMessage());
			}else
			{
				e.printStackTrace();
				payload.setValue("internal error");
			}
			response.setPayload(payload);
			response.setResult(ResultCode.COMPONENT_ERROR);
		}
	}
	
	private void queryAll(Request request,Response response)throws ExecuteException
	{
		SurgeryRecordSearchBean searchBean = setSearchBean(request);

		String customer = request.getRequestParameters().get("customer");
		String customerId = request.getRequestParameters().get("customerId");
		
		CustomerService customerService = new CustomerService();
		
		if(!Utils.isStringEmptyWithTrim(customer))
		{
			
			List<Customer> customers = customerService.search(customer, request.getAccount().getCompany());
			if(customers == null || customers.isEmpty())
			{
				emptyReply(response);
				return;
			}
			searchBean.setCustomer(customers);
		}
		if(!Utils.isStringEmptyWithTrim(customer))
		{
			int id = Integer.parseInt(customerId);
			Customer c = customerService.queryCustomer(id);
			if(c == null || c.getCompany()!= request.getAccount().getCompany())
			{
				emptyReply(response);
				return;
			}
			List<Customer> cus = new ArrayList<Customer>();
			cus.add(c);
			searchBean.setCustomer(cus);
		}
		JSONListDTO list = query(searchBean);
		reply(response,list);
	}
	private JSONListDTO query(SurgeryRecordSearchBean searchBean)
	{
		SurgeryRecordService service = new SurgeryRecordService();
		
		List<SurgeryRecord> records = service.querySurgeryRecordPagination(searchBean);
		int total = service.querySurgeryRecordPaginationCount(searchBean);
		
		JSONListDTO list = new JSONListDTO();
		list.setTotal(total);
		list.setRows(records);
		return list;
	}
	private SurgeryRecordSearchBean setSearchBean(Request request)
	{
		int count = getCountPerPage(request);
		int start = getCountStart(request);
		
		SurgeryRecordSearchBean searchBean = new SurgeryRecordSearchBean();
		searchBean.setCompany(request.getAccount().getCompany());
		searchBean.setLimit(count);
		searchBean.setStart(start);
		

		
		return searchBean;
	}
	private int getCountPerPage(Request request)
	{
		String count = request.getRequestParameters().get("size");
		if(count == null || "".equals(count.trim()))return 10;
		try
		{
			return Integer.parseInt(count);
		}catch(Exception e)
		{
			e.printStackTrace();
			return 10;
		}
	}
	private int getCountStart(Request request)
	{
		String start = request.getRequestParameters().get("start");
		if(start == null || "".equals(start.trim()))return 0;
		try
		{
			return Integer.parseInt(start);
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}
	private void reply(Response response,JSONListDTO dto)
	{
		ResponsePayload payload = new ResponsePayload(JSON.toJSONString(dto));
		response.setPayload(payload);
	}
	private void emptyReply(Response response)
	{
		JSONListDTO list = new JSONListDTO();
		list.setTotal(0);
		list.setRows(new ArrayList<SurgeryRecord>());
		
		reply(response,list);
	}
}
